package com.longxi.tianjintianbao.ui.activity.assetscheck;

import android.Manifest;
import android.content.Intent;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.controller.DataInventoryController;
import com.longxi.tianjintianbao.greedao.entity.InventoryChild;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.presenter.assetscheck.Test2Presenter;
import com.longxi.tianjintianbao.ui.view.assetscheck.ITest2AtView;
import com.luck.picture.lib.permissions.RxPermissions;
import com.luck.picture.lib.tools.PictureFileUtils;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class TestActivity2 extends BaseActivity<ITest2AtView, Test2Presenter> implements ITest2AtView {

    @BindView(R.id.nestedscrollview)
    NestedScrollView nestedscrollview;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.llEdit)
    LinearLayout llEdit;
    @BindView(R.id.etPdsl)
    EditText etPdsl;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btnPd)
    Button btnPd;
    @BindView(R.id.ivImage1)
    ImageView ivImage1;
    @BindView(R.id.ivImage2)
    ImageView ivImage2;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_test2;
    }

    @Override
    public void initView() {
        super.initView();

        // 清空图片缓存，包括裁剪、压缩后的图片 注意:必须要在上传完成后调用 必须要获取权限
        RxPermissions permissions = new RxPermissions(this);
        permissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Boolean aBoolean) {
                if (aBoolean) {
                    PictureFileUtils.deleteCacheDirFile(TestActivity2.this);
                } else {
                    Toast.makeText(TestActivity2.this,
                            getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

        mPresenter.initTakePicture(recyclerView, 2);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.setBar();
    }

    @Override
    public void initListener() {
        super.initListener();
        btnPd.setOnClickListener(v -> mPresenter.onClickBtn());
    }

    @Override
    protected Test2Presenter createPresenter() {
        return new Test2Presenter(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode != RESULT_OK) {
//            return;
//        }
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public NestedScrollView getNestedscrollview() {
        return nestedscrollview;
    }

    @Override
    public TextView getTvName() {
        return tvName;
    }

    @Override
    public LinearLayout getLlEdit() {
        return llEdit;
    }

    @Override
    public EditText getEtPdsl() {
        return etPdsl;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public Button getBtnPd() {
        return btnPd;
    }

    @Override
    public ImageView getIvImage1() {
        return ivImage1;
    }

    @Override
    public ImageView getIvImage2() {
        return ivImage2;
    }

    @Override
    protected void onDestroy() {
        InventoryChild inventoryChild = DataInventoryController.getInventoryChildById(getIntent().getLongExtra(AppConst.BILLID, 0));
        if("0".equals(inventoryChild.isdraft) && "1".equals(inventoryChild.state)){
            DataInventoryController.DeleteInventoryChild(inventoryChild);
        }
        super.onDestroy();
    }
}
